﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace FatZombieAttack
{
    public class Bullets
    {
        public List<Bullets> bullets;
        double Bulletspeed;
        public int Damage;
        public float Points;
        Vector2 bulletposition;
        private Texture2D bulletImage;
        double DeltaX;
        double DeltaY;
        int direction;
        bool shootready = true;
        public void Initialize()
        {
            Bulletspeed = 700f;
            Damage = 20;
        }
        public virtual void LoadContent(ContentManager Content)
        {
            bullets = new List<Bullets>();
            bulletImage = Content.Load<Texture2D>("Sprites/Bullet");


        }
        public virtual void Update(GameTime gameTime,Player p,Map m)
        {
            MouseState mouse = Mouse.GetState();
            if (mouse.LeftButton == ButtonState.Pressed && shootready == true)
            {
                Bullets bullet = new Bullets();
                bullets.Add(bullet);
                bullet.bulletposition = p.getmidplayerPosition();
                double a = p.getmidplayerPositionx() - mouse.X;
                double b = p.getmidplayerPositiony() - mouse.Y;
                bullet.Damage = 20;
                if (a == 0 && b > 0)
                    bullet.direction = 0;
                else if (a == 0 && b < 0)
                    bullet.direction = 2;
                else if (a > 0 && b == 0)
                    bullet.direction = 0;
                else if (a < 0 && b == 0)
                    bullet.direction = 3;
                else if (a > 0 && b > 0)
                    bullet.direction = 0;
                else if (a < 0 && b > 0)
                    bullet.direction = 1;
                else if (a > 0 && b < 0)
                    bullet.direction = 2;
                else
                    bullet.direction = 3;
                double c = b / a;
                if (c > 0)
                    bullet.DeltaX = 1 / (c + 1);
                else
                    bullet.DeltaX = 1 / ((-c) + 1);
                bullet.DeltaY = 1 - bullet.DeltaX;
                
            }
            for (int i = 0; i < bullets.Count; i++)
            {
                double x = bullets[i].bulletposition.X;
                double y = bullets[i].bulletposition.Y;
                if (bullets[i].direction == 0)
                {
                    x -= Bulletspeed * bullets[i].DeltaX * (double)gameTime.ElapsedGameTime.TotalSeconds;
                    y -= Bulletspeed * bullets[i].DeltaY * (double)gameTime.ElapsedGameTime.TotalSeconds;
                }
                else if (bullets[i].direction == 1)
                {
                    x += Bulletspeed * bullets[i].DeltaX * (double)gameTime.ElapsedGameTime.TotalSeconds;
                    y -= Bulletspeed * bullets[i].DeltaY * (double)gameTime.ElapsedGameTime.TotalSeconds;
                }
                else if (bullets[i].direction == 2)
                {
                    x -= Bulletspeed * bullets[i].DeltaX * (double)gameTime.ElapsedGameTime.TotalSeconds;
                    y += Bulletspeed * bullets[i].DeltaY * (double)gameTime.ElapsedGameTime.TotalSeconds;
                }
                else
                {
                    x += Bulletspeed * bullets[i].DeltaX * (double)gameTime.ElapsedGameTime.TotalSeconds;
                    y += Bulletspeed * bullets[i].DeltaY * (double)gameTime.ElapsedGameTime.TotalSeconds;
                }
                
                bullets[i].bulletposition = new Vector2((float)x,(float)y);
               
            }

        }
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < bullets.Count; i++)
            {
                spriteBatch.Draw(bulletImage, bullets[i].bulletposition, Color.White);
            }
        }

        public bool BulletonZombie(float x, float y, Zombie z)
        {
            float a;
            if (z.GetSize() == 0)
                a = 30;
            else if (z.GetSize() == 1)
                a = 60;
            else
                a = 80;

            if ((x < z.getzombiePositionx() || y < z.getzombiePositiony() || x > (z.getzombiePositionx() + a) || y > (z.getzombiePositiony() + a)))
                return true;
            else
                return false;

           
        }

        public float getbulletPositionx() { return bulletposition.X; }
        public float getbulletPositiony() { return bulletposition.Y; }
        public float getDamage() { return Damage; }
    }
}
